package com.kc.sjwh.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Jqxz;
import com.kc.sjwh.entity.vo.table.Table;
import com.kc.sjwh.entity.vo.table.Td;
import com.kc.sjwh.entity.vo.table.Tr;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.Tool;
import com.kc.sjwh.service.JqxzService;

/**
 * 警情性质管理action
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Controller
@RequestMapping("jqxz")
public class JqxzAction extends BaseAction {
	static Logger logger = Logger.getLogger(JqxzAction.class);

	@Autowired
	JqxzService service;
	
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "jqxz/jqxzList";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<Jqxz> page = service.getPage(pageSize, pageNum, super.getParamMap(req));
		
		//数据转表格table
		Table table = new Table();
		table.add(new Tr("编号", "简称", "操作"));//表头
		for(Jqxz one : page.getList()) {//数据行
			table.add(new Tr(
				new Td(one.getJqxzbh()),
				new Td(one.getJqxzjc()),
				new Td("<a  data-toggle='modal' data-iframe='form?id="+one.getJqxzbh()+"' data-size='lg'>修改</a> &nbsp; " + 
						//"<a href='form?id="+one.getJqxzbh()+"'>修改</a> &nbsp; "+ 
						"<a onclick=\"deletes('"+one.getJqxzbh()+"')\">删除</a>")
			));
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		return "jqxz/jqxzForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req, Jqxz one, String act) throws Exception {
		one.setJqxzjcpym(Tool.transToPinyin(one.getJqxzjc()));//拼音
		one.setBzjqxzmc(one.getJqxzjc());
		one.setJqxzqc(one.getJqxzjc());
		if("新增".equals(act)) {
			service.add(one, getLoginUser().getMc());
		} else {
			service.update(one, getLoginUser().getMc());
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
	
}
